package com.interview.ujiuye9;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Copyright (C), 2018-2020
 * FileName: MyLogInvocaitonHandler
 * Author:   kongfanyu
 * Date:     2020/11/11 15:48
 */
public class MyLogInvocaitonHandler  implements InvocationHandler {
    private Object object;
    public MyLogInvocaitonHandler(Object object){
        this.object = object;
    }
    /**
     *  调用真实对象的方法
     * @param proxy 代理对象
     * @param method  执行的真实目标方法
     * @param args    方法的参数
     * @return        方法执行的结果
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();
        before(methodName);
        System.out.println("222222");
        Object result = method.invoke(object,args);//调用被代理对象方法
        after(methodName);
        return result;
    }

    public void before(String name){
        System.out.println("开始执行" + name +"方法");
    }

    public void after(String name){
        System.out.println("结束执行:"+name+"方法");
    }

}
